import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getInfo
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
	state: {
		token: getToken(),
		name: storage.get(constant.name),
		nickName: storage.get(constant.nickName),
		userId: storage.get(constant.userId),
		userType: storage.get(constant.userType),
		deptId: storage.get(constant.deptId),
		deptName: storage.get(constant.deptName),
		avatar: storage.get(constant.avatar),
		roles: storage.get(constant.roles),
		permissions: storage.get(constant.permissions)
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_NICKNAME: (state, nickName) => {
			state.nickName = nickName
			storage.set(constant.nickName, nickName)
		},
		SET_USERID: (state, userId) => {
			state.userId = userId
			storage.set(constant.userId, userId)
		},
		SET_USERTYPE: (state, userType) => {
			state.userType = userType
			storage.set(constant.userType, userType)
		},
		SET_DEPTID: (state, deptId) => {
			state.deptId = deptId
			storage.set(constant.deptId, deptId)
		},
		SET_DEPTNAME: (state, deptName) => {
			state.deptName = deptName
			storage.set(constant.deptName, deptName)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
			storage.set(constant.roles, roles)
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
			storage.set(constant.permissions, permissions)
		}
	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			return new Promise((resolve, reject) => {
				login(userInfo).then(res => {
					let data=res.data
					// if (res.departmentId) {
					// 	resolve(res.departmentId)
					// 	return
					// }
					setToken(data.token)
					commit('SET_TOKEN', data.token)
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					const user = res.data.user
					const nickName = user.nickName
					const userId = user.userId
					const userType = user.userType
					const deptName = user.departmentName
					const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
						require("@/static/images/profile.png") : baseUrl + user.avatar
					const username = user.userName
					commit('SET_NAME', username)
					commit('SET_NICKNAME', nickName)
					commit('SET_USERID', userId)
					commit('SET_USERTYPE', userType)
					commit('SET_DEPTNAME', deptName)
					commit('SET_AVATAR', avatar)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_TOKEN', '')
					commit('SET_ROLES', [])
					commit('SET_PERMISSIONS', [])
					removeToken()
					storage.clean()
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}

export default user